import * as AuthenticationService from 'services/AuthenticationService';
import * as commonService from 'services/commonService';
import { COUNT_DOWN } from 'utils/Enum'
import { Toast } from 'antd-mobile';
export default {
  namespace: 'AuthenticationModel',
  state: {
    isSuccess: '',  //显示页面是成功还是失败
    count: COUNT_DOWN.RESULT_TIME,
    bizNo: '',  //判断是否是支付宝
    userWsid: '',
    session: '',
    type: '',
    status: '',
    Certification: '',
    returnUrl: '',
    enterType: '',
    haveToAlipay: '',
  },
  effects: {
    *getAlipayDetail({ payload, callback }, { call, put }) {
      const response = yield call(AuthenticationService.getAlipayDetail, payload);
      let reg = new RegExp(/0000$/, 'i')
      let { code } = response
      //如果返回的code是以 0000 结尾 就代表成功
      if (reg.test(code)) {
        const { isSuccess, captcha } = response
        localStorage.setItem('Captcha', captcha)

        yield put({
          type: 'setIsSuccess',
          payload: isSuccess
        })
      } else {
        Toast.fail("获取支付宝信息失败！")
        yield put({
          type: 'setIsSuccess',
          payload: false
        });
      }
      //开始倒计时
      yield callback()
    },
    *getIntendCertificationResult({ payload, callback }, { call, put }) {
      const response = yield call(commonService.getIntendCertificationResult, payload);
      let reg = new RegExp(/0000$/, 'i')
      let { code } = response
      //如果返回的code是以 0000 结尾 就代表成功
      if (reg.test(code)) {
        const { isPassed, captcha } = response
        localStorage.setItem('Captcha', captcha)
        yield put({
          type: 'setIsSuccess',
          payload: isPassed
        })
      } else {
        Toast.fail("获取认证结果失败！")
        yield put({
          type: 'setIsSuccess',
          payload: false
        });
      }
      //开始倒计时
      yield callback()
    }
  },
  reducers: {
    setQuery(state, { payload }) {
      return {
        ...state,
        ...payload
      }
    },
    countDown(state, { payload }) {
      return {
        ...state,
        count: payload
      }
    },
    setIsSuccess(state, { payload }) {
      return {
        ...state,
        isSuccess: payload
      }
    }
  },
	/**
	 * 当支付宝的结果返回后设置页面显示成功还是失败
	 */
  subscriptions: {
    /**
     * 用来获取路由参数 放置在state中
     */
    getQuery({ dispatch, history }) {
      history.listen(location => {
        let {
          pathname,
          query
        } = location
        if (pathname === '/Authentication') {
          let {
            bizNo,
            userWsid,
            session,
            type, //判断企业用户还是个人用户
            status,
            Certification,
            returnUrl,
            enterType, envelopeWsid, participantWsid,
            haveToAlipay
          } = query
          dispatch({
            type: 'setQuery',
            payload: {
              bizNo,
              userWsid,
              session,
              type,
              status,
              Certification,
              returnUrl,
              enterType, envelopeWsid, participantWsid, haveToAlipay
            }
          })
        }
      })
    }
  }
};
